/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Report3.java
 *
 * Created on Feb 25, 2013, 10:27:53 PM
 */
package membershipdemo;

import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.util.JRProperties;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

/**
 *
 * @author $alama
 */
public class Report3 extends javax.swing.JFrame {

    int type = 0;
    private EntityManager entityManager;
    private Query query;
    private MemberShipDemoView memberShipDemoView;
    /** Creates new form Report3 */
    public Report3(MemberShipDemoView memberShipDemoView) {
        this.memberShipDemoView=memberShipDemoView;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();

        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(membershipdemo.MemberShipDemoApp.class).getContext().getResourceMap(Report3.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText(resourceMap.getString("jRadioButton1.text")); // NOI18N
        jRadioButton1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jRadioButton1.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jRadioButton1.setName("jRadioButton1"); // NOI18N
        jRadioButton1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButton1ItemStateChanged(evt);
            }
        });

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setSelected(true);
        jRadioButton2.setText(resourceMap.getString("jRadioButton2.text")); // NOI18N
        jRadioButton2.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jRadioButton2.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jRadioButton2.setName("jRadioButton2"); // NOI18N
        jRadioButton2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButton2ItemStateChanged(evt);
            }
        });

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addComponent(jLabel1)
                .addContainerGap(105, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(105, 105, 105)
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
                .addComponent(jRadioButton2)
                .addGap(123, 123, 123))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(248, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(195, 195, 195))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(jLabel1)
                .addGap(51, 51, 51)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2))
                .addGap(52, 52, 52)
                .addComponent(jButton1)
                .addContainerGap(80, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jRadioButton2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jRadioButton2ItemStateChanged
        type = 0;        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton2ItemStateChanged

    private void jRadioButton1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jRadioButton1ItemStateChanged
        type = 1;        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton1ItemStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        generate();        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    this.memberShipDemoView.enable_Report_3_Button();
}//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new Report3().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    // End of variables declaration//GEN-END:variables

    public void generate() {
        try {

            org.jdesktop.application.ResourceMap resourceMap1 = org.jdesktop.application.Application.getInstance(membershipdemo.MemberShipDemoApp.class).getContext().getResourceMap(MemberShipDemoView.class);

            entityManager = javax.persistence.Persistence.createEntityManagerFactory(resourceMap1.getString("entityManager.persistenceUnit")).createEntityManager();

            query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap1.getString("query.query")); // NOI18N
            entityManager.getTransaction().begin();
            java.util.Collection data = query.getResultList();

            Map parameters = new HashMap();
            parameters.put("image", "ac.jpg");
            FileInputStream inputStream = new FileInputStream("report3.jrxml");
            JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            ArrayList<Report3Bean> memberses = new ArrayList<Report3Bean>();
            for (Iterator it = data.iterator(); it.hasNext();) {
                Members object = (Members) it.next();
                if (type == 0) {
                    parameters.put("title", "صرفوا اعانات");
                    System.out.println("hdhdhdhh");
                    if (!object.getSubventionList().isEmpty()) {
                        System.out.println("hdhdhdh2h");
                        for (Iterator it1 = object.getSubventionList().iterator(); it1.hasNext();) {
                            Subvention object1 = (Subvention) it1.next();
                            memberses.add(new Report3Bean(object.getId(), object.getMemId(), object.getMemName(), object1.getServiceType(), object1.getAmount()));
                        }

                    }
                } else if (type == 1) {
                    parameters.put("title", "لم يصرفوا اعانات");
                    if (object.getSubventionList().isEmpty()) {
                        memberses.add(new Report3Bean(object.getId(), object.getMemId(), object.getMemName(), "", 0));

                    }
                }
            }
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JRBeanArrayDataSource(memberses.toArray()));
            JRHtmlExporter jRPdfExporter = new JRHtmlExporter();
            jRPdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

            JRProperties.setProperty("net.sf.jasperreports.default.pdf.encoding", "UTF-8");

            jRPdfExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");

            jRPdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
                    "report2.html");
            jRPdfExporter.exportReport();
            Desktop.getDesktop().open(new File("report2.html"));
        } catch (JRException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}
